from django.db.models import F
from apps.user.models import UserModel


# 更新直推成员营业额
def updateVerticalPerformance(scene, parent, n):
    print('更新', scene, parent.id, n)
    if parent.id:
        # 更新团队总人数
        _parent = UserModel.objects.get(id=parent.id)
        if scene == 'add':
            _parent.verticalPerformance = F('verticalPerformance') + n
            _parent.save()
        elif scene == 'sub':
            if _parent.verticalPerformance >= n:
                _parent.verticalPerformance = F('verticalPerformance') - n
                _parent.save()
            else:
                _parent.verticalPerformance = 0
                _parent.save()
    else:
        return False


# 更新直推人数
def updateVerticalPeopleNumber(scene, parent, n):
    print('更新', scene, parent.id, n)
    if parent.id:
        # 更新团队总人数
        _parent = UserModel.objects.get(id=parent.id)
        if scene == 'add':
            _parent.verticalPeopleNumber = F('verticalPeopleNumber') + n
            _parent.save()
        elif scene == 'sub':
            if _parent.verticalPeopleNumber >= n:
                _parent.verticalPeopleNumber = F('verticalPeopleNumber') - n
                _parent.save()
            else:
                _parent.verticalPeopleNumber = 0
                _parent.save()

    else:
        return False


# 更新直推订单数
def updateVerticalOrderNumber(scene, parent, n):
    print('更新', scene, parent.id, n)
    if parent.id:
        # 更新团队总人数
        _parent = UserModel.objects.get(id=parent.id)
        if scene == 'add':
            _parent.verticalOrderNumber = F('verticalOrderNumber') + n
            _parent.save()
        elif scene == 'sub':
            if _parent.verticalOrderNumber >= n:
                _parent.verticalOrderNumber = F('verticalOrderNumber') - n
                _parent.save()
            else:
                _parent.verticalOrderNumber = 0
                _parent.save()

    else:
        return False